﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Input;
using SimpleMvvmToolkit;
using BuyTicket.Service;
using BuyTicket.Views;
using HtmlAgilityPack;

namespace BuyTicket.Models
{
    class Ticket
    {
        public Ticket(string no,
            string start,
            string end,
            string requestArgs)
        {
            this.No = no;
            this.Start = start;
            this.End = end;
            this.RequestArgs = requestArgs;
        }

        public string No { get; set; }

        public string Start { get; set; }

        public string End { get; set; }

        public string RequestArgs { get; set; }


        ICommand m_OrderCommand = null;
        public ICommand OrderCommand
        {
            get
            {
                return m_OrderCommand ?? (m_OrderCommand = new DelegateCommand(Order));
            }
        }

        void Order()
        {
            string strHtml;
            bool ordered = BuyTicketService.Order(this, out strHtml);

            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(strHtml);
            //解析出提交参数
            List<string> seats = new List<string>();
            var node = document.DocumentNode.SelectSingleNode("//select[@id='passenger_1_seat']");
            if (node != null)
            {
                var options = node.SelectNodes(".//option");
                if (options != null)
                {
                    foreach (var option in options)
                    {
                        seats.Add(option.InnerText);
                    }
                }
            }

            List<string> tickets = new List<string>();
            node = document.DocumentNode.SelectSingleNode("//select[@id='passenger_1_ticket']");
            if (node != null)
            {
                var options = node.SelectNodes(".//option");
                if (options != null)
                {
                    foreach (var option in options)
                    {
                        seats.Add(option.InnerText);
                    }
                }
            }

            StringBuilder bulider = new StringBuilder();
            bulider.AppendLine(string.Format("车次：{0}", this.No));
            bulider.AppendLine(string.Format("起始：{0} 终点:{1}",
                this.Start, this.End));

            //打开SubmitWindow
            SubmitWindow submit = new SubmitWindow();
            submit.SetArgs(new SubmitArgs(bulider.ToString(),seats, tickets));
            submit.Topmost = true;
            submit.ShowDialog();
        }

        public override string ToString()
        {
            string[] sArry = Regex.Split(RequestArgs, "#");
            return string.Format("station_train_code={0}&train_date={1}&seattype_num=&from_station_telecode={2}&to_station_telecode={3}&include_student={4}&from_station_telecode_name={5}&to_station_telecode_name={6}&round_train_date={7}&round_start_time_str=00%3A00--24%3A00&single_round_type=1&train_pass_type=QB&train_class_arr=QB%23D%23Z%23T%23K%23QT%23&start_time_str=00%3A00--24%3A00&lishi={8}&train_start_time={9}&trainno={10}&arrive_time={11}&from_station_name={12}&to_station_name={13}&ypInfoDetail={14}",
                sArry[0],
                Global.MyTicketArgs.Date.ToString("yyyy-MM-dd"),
                sArry[4],
                sArry[5],
                Global.MyTicketArgs.IsStudent?"0X00":"00",
                Utility.UrlEncode(Global.MyTicketArgs.StartStation.ChineseName),
                Utility.UrlEncode(Global.MyTicketArgs.EndStation.ChineseName),
                DateTime.Now.ToString("yyyy-MM-dd"),
                Utility.UrlEncode(sArry[1]),
                Utility.UrlEncode(sArry[2]),
                sArry[3],
                Utility.UrlEncode(sArry[6]),
                Utility.UrlEncode(sArry[7]),
                Utility.UrlEncode(sArry[8]),
                sArry[9]);
        }
    }
}
